<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Message_model {

	// 公告列表  。。。缩略
	public function getindex() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '发送公告') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/getAbnotice',
				'log_desc' => '浏览公告消息',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		// 查询该id所对应的角色，组织，人员
		$oid = Db::table('cn_user_organization_relation')
			->alias('cuor')
			->join('cn_organization co', 'cuor.org_id = co.org_id')
			->where('cuor.user_id', $uid)
			->field('cuor.org_id')
			->find();
		$cid = Db::table('cn_user_character_relation')
			->alias('cucr')
			->join('cn_character cc', 'cucr.chara_id = cc.chara_id')
			->where('cucr.user_id', $uid)
			->field('cucr.chara_id')
			->find();
		// $omail = Db::table('cn_mail_addressee')
		//         ->alias('cma')
		//         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
		//         ->where('cma.add_org_id',$oid['org_id'])
		//         ->where('cm.mail_send_channel','eq','2')
		//         ->where('mail_send_type','eq','1')
		//         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
		//         ->order('create_time desc')
		//         ->select();
		// if ($oid['org_id'] == '') {
		// 	$sql = "SELECT
		//                   cm.mail_title,
		//                   cm.mail_show_content,
		//                   cm.create_time,
		//                   cm.mail_id,
		//                   cms.status
		//               FROM
		//                   cn_mail_addressee AS cma
		//                   LEFT JOIN cn_mail AS cm ON cm.mail_id = cma.mail_id
		//                   LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//               WHERE
		//                   cm.mail_send_channel = 2
		//                   AND cms.user_id = " . $uid . "
		//                   AND mail_send_type = 1
		//                   AND cma.is_delete = 0
		//                   AND cm.is_delete = 0

		//               ORDER BY
		//                   create_time DESC";
		// } else {
		$sql = "SELECT
                    cm.mail_title,
                    cm.mail_show_content,
                    cm.create_time,
                    cm.mail_id,
                    cms.status
                FROM
                    cn_mail_addressee AS cma
                    LEFT JOIN cn_mail AS cm ON cm.mail_id = cma.mail_id
                    LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                WHERE
                    cms.user_id = " . $uid . "
                    AND cm.mail_send_channel = 2
                    AND mail_send_type = 1
                    AND cma.is_delete = 0
                    AND cm.is_delete = 0
                ORDER BY
                    create_time DESC";

		// }
		$omail = Db::query($sql);
		// $cmail = Db::table('cn_mail_addressee')
		//         ->alias('cma')
		//         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
		//         ->where('cma.add_chara_id',$cid['chara_id'])
		//         ->where('cm.mail_send_channel','eq','2')
		//         ->where('mail_send_type','eq','1')
		//         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
		//         ->order('create_time desc')
		//         ->select();

		// $umail = Db::table('cn_mail_addressee')
		//         ->alias('cma')
		//         ->join('cn_mail cm','cm.mail_id = cma.mail_id')
		//         ->where('cma.add_user_id',$uid)
		//         ->where('cm.mail_send_channel','eq','2')
		//         ->where('mail_send_type','eq','1')
		//         ->field('cm.mail_title,cm.mail_show_content,cm.create_time,cm.mail_id,cm.is_read')
		//         ->order('create_time desc')
		//         ->select();

		// dump($omail);die;
		$abnotice = $omail;
		// $page = $abnotice->render();
		if (empty($abnotice)) {
			$amount = 0;
		}
		$amount = $abnotice;

		$ores = Db::table('cn_mail')
			->alias('cm')
			->join('cn_mail_addressee cma', 'cma.mail_id = cm.mail_id')
			->where('cma.add_org_id', $oid['org_id'])
			->where('cm.is_delete', 'eq', 0)
			->where('cm.is_read', 'eq', 0)
			->where('cm.mail_send_channel', 'eq', 2)
			->field('cm.mail_title,cm.mail_id,count(cm.mail_id)as count,cm.mail_content,cm.create_time,cm.mail_id,cm.is_read')
			->group('cm.mail_id')
			->select();
		// dump($ores);die;
		// if ($oid['org_id']) {
		// 	$wsql = "SELECT
		//                       cm.mail_title,
		//                       cm.mail_id,
		//                       count( cm.mail_id ) AS count,
		//                       cm.mail_content,
		//                       cm.create_time,
		//                       cm.mail_id,
		//                       cms.STATUS
		//                   FROM
		//                       cn_mail AS cm
		//                       LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
		//                       LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//                   WHERE
		//                       cma.add_org_id = " . $oid['org_id'] . "
		//                       AND cm.is_delete = 0
		//                       AND cms.STATUS = 0
		//                       AND cm.mail_send_channel = 2
		//                       AND cms.user_id = " . $uid . "
		//                       and cms.status = 0
		//                   GROUP BY
		//                       cm.mail_id";
		// } else {
		$wsql = "SELECT
                        cm.mail_title,
                        cm.mail_id,
                        count( cm.mail_id ) AS count,
                        cm.mail_content,
                        cm.create_time,
                        cm.mail_id,
                        cms.STATUS
                    FROM
                        cn_mail AS cm
                        LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
                        LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                    WHERE
                        cms.user_id = " . $uid . "
                        AND cm.is_delete = 0
                        AND cms.STATUS = 0
                        AND cm.mail_send_channel = 2
                        and cms.status = 0
                    GROUP BY
                        cm.mail_id";
		// }
		$wres = Db::query($wsql);

		$num2 = (count($wres));
		// var_dump($num2);die;

		// if (empty($num)) {
		// 	$num = 0;
		// 	if (empty($num2)) {
		// 		$num2 = 0;
		// 		$info = [
		// 			'notice' => $amount,
		// 			'list' => $list,
		// 			'num2' => $num2,
		// 			// 'page' => $page
		// 		];
		// 	}
		// 	$info = [
		// 		'notice' => $amount,
		// 		'list' => $list,
		// 		'num2' => $num2,
		// 		// 'page' => $page
		// 	];
		// } else {
		$info = [
			'notice' => $amount,
			'list' => $list,
			'num2' => $num2,
			// 'page' => $page
		];
		// }
		// dump($info);die;
		return $info;
	}

	public function getNotice() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '写公告') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/getNotice',
				'log_desc' => '写公告',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		//获取所有人员、角色、组织（去除自己）
		// $alluser = Db::table('cn_user')
		//         ->where('user_id','neq',$uid)
		//         ->field('user_name')
		//         ->select();
		// for ($i=0; $i < count($alluser) ; $i++) {
		//     $au[$i]['name'] = $alluser[$i]['user_name'];
		// }

		$bmparent = Db::table('cn_organization')
			->where('is_delete', '0')
			->orderRaw('concat(path,org_id,",")')
			->select();
		// var_dump($org);die;
		$all = array();
		foreach ($bmparent as $k => $v) {
			// 统计多少级分类
			$level = substr_count($v['path'], ','); // 统计','出现的次数
			$v['level'] = $level;
			$v['html'] = str_repeat('---', $level - 1); // str_repeat(重复字符串,重复次数)
			$all[] = $v;
		}
		//加入所有部门
		$top[] = [
			'org_id' => 0,
			'org_name' => '所有部门',
		];
		$all = array_merge($top, $all);
		// dump($all);die;
		// dump($top);die;
		// for ($i=0; $i < count($allorg) ; $i++) {
		//     $ao[$i]['name'] = $allorg[$i]['org_name'];
		// }
		// // var_dump($ao);die;
		//         // var_dump($allorg);die;
		// // foreach($allorg as $j=>$r){
		// //     $m[$j] = $r['org_name'];
		// //     $m[$j] = $r['org_name'];
		// // }
		// // 信息合并到一个二维数组
		// $all = $ao;
		// var_dump($all);die;
		// 查询该id所对应的角色，组织，人员
		$oid = Db::table('cn_user_organization_relation')
			->alias('cuor')
			->join('cn_organization co', 'cuor.org_id = co.org_id')
			->where('cuor.user_id', $uid)
			->field('cuor.org_id')
			->find();
		// var_dump($oid);die;
		$cid = Db::table('cn_user_character_relation')
			->alias('cucr')
			->join('cn_character cc', 'cucr.chara_id = cc.chara_id')
			->where('cucr.user_id', $uid)
			->field('cucr.chara_id')
			->find();

		// var_dump($num);die;
		// if ($oid['org_id']) {
		// 	$wsql = "SELECT
		//                       cm.mail_title,
		//                       cm.mail_id,
		//                       count( cm.mail_id ) AS count,
		//                       cm.mail_content,
		//                       cm.create_time,
		//                       cm.mail_id,
		//                       cms.STATUS
		//                   FROM
		//                       cn_mail AS cm
		//                       LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
		//                       LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//                   WHERE
		//                       cma.add_org_id = " . $oid['org_id'] . "

		//                       AND cm.is_delete = 0
		//                       AND cms.STATUS = 0
		//                       AND cm.mail_send_channel = 2
		//                       AND cms.user_id = " . $uid . "
		//                       and cms.status = 0
		//                   GROUP BY
		//                       cm.mail_id";
		// } else {
		$wsql = "SELECT
                        cm.mail_title,
                        cm.mail_id,
                        count( cm.mail_id ) AS count,
                        cm.mail_content,
                        cm.create_time,
                        cm.mail_id,
                        cms.STATUS
                    FROM
                        cn_mail AS cm
                        LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
                        LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                    WHERE
                        cms.user_id = " . $uid . "
                        AND cm.is_delete = 0
                        AND cms.STATUS = 0
                        AND cm.mail_send_channel = 2
                        and cms.status = 0
                    GROUP BY
                        cm.mail_id";
		// }
		$wres = Db::query($wsql);
		$num2 = (count($wres));

		// if (empty($num)) {
		//     $num = 0;
		//     if (empty($num)) {
		//         $num2 = 0;
		//         $info = [
		//         'all' => $all,
		//         'node' => $list,
		//         'num2' => $num2
		//     ];
		//     }
		//      $info = [
		//         'all' => $all,
		//         'node' => $list,
		//         'num2' => $num2
		//     ];
		// }else{
		$info = [
			'all' => $all,
			'node' => $list,
			'num2' => $num2,
		];
		// }

		return $info;
	}

	public function sendNotice() {
		// dump($_POST);die;
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '发送公告') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/sendNotice',
				'log_desc' => '发送公告',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();

		//写入cn_mail
		if (empty($_POST['editorValue'])) {
			$_POST['editorValue'] = '';
		}
		$data = [
			'mail_title' => $_POST['subject'],
			'mail_content' => $_POST['editorValue'],
			'mail_file' => '0',
			'mail_send_type' => '1',
			// 'mail_type' => $_POST['type'],
			'mail_secrecy' => '0',
			'mail_send_channel' => '2',
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_read' => '0',
			'is_delete' => '0',
		];
		$res = db('_mail')
			->insertGetId($data);

		// $to = $_POST['to'];
		$to = array('所有部门');
		// dump($to);die;
		// $t = $to[0];
		for ($i = 0; $i < count($to); $i++) {
			# code...
			if ($to[$i] == '所有部门') {
				$org_id[0] = '-1';

			} else {

				// 根据收件人名获取id
				$req[$i] = Db::table('cn_organization')
					->alias('co')
					->where('co.org_name', $to[$i])
					->field('org_id')
					->find();
				// var_dump($req);die;
				$org_id[$i] = $req[$i]['org_id'];
			}
		}
		$p = [];
		$arr = [];
		for ($j = 0; $j < count($org_id); $j++) {

			$info[$j] = [
				'add_type' => '1',
				'add_org_id' => $org_id[$j],
				'add_chara_id' => '0',
				'add_user_id' => '0',
				'mail_id' => $res,
				'create_user_id' => $uid,
				'create_time' => $time,
				'update_user_id' => $uid,
				'update_time' => $time,
				'is_delete' => '0',
			];
			//根据org_id查到人员
			$log[$j] = '';
			if ($org_id[$j] == '-1') {
				$code[$j] = Db::table('cn_user_organization_relation')
					->alias('cuor')
					->join('cn_user cu', 'cu.user_id = cuor.user_id')
					->field('cu.user_id')
					->select();
			} else {
				$code[$j] = Db::table('cn_user_organization_relation')
					->alias('cuor')
					->join('cn_user cu', 'cu.user_id = cuor.user_id')
					->where('cuor.org_id', $org_id[$j])
					->field('cu.user_id')
					->select();
			}
			//添加人员公告状态表
			$a = db('_mail_addressee')
				->insert($info[$j]);
			for ($k = 0; $k < count($code[$j]); $k++) {
				$log[$j] .= $code[$j][$k]['user_id'] . ',';
			}
			$log[$j] = rtrim($log[$j], ',');

		}
		for ($c = 0; $c < count($code); $c++) {
			$p = array_values($code[$c]);
			// dump(array_values($p));
			$arr = array_merge($p, $arr);
		}
		for ($i = 0; $i < count($arr); $i++) {
			$arr[$i] = $arr[$i]['user_id'];
		}

		for ($p = 0; $p < count($arr); $p++) {
			$status = [
				'mail_id' => $res,
				'user_id' => $arr[$p],
				'status' => 0,
			];
			//添加消息状态表
			$result = Db::table('cn_mail_status')
				->insert($status);
		}

		if ($result && $res && $a) {
			$b = [
				'a' => $result,
				'node' => $list,
			];
			return $b;
		} else {
			return false;
		}

	}

	// 公告消息搜索
	public function getnoticeSearch() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '公告消息搜索') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/getnoticeSearch',
				'log_desc' => '公告消息搜索',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		$keyword = $_GET['search'];
		$res = Db::table('cn_mail')
			->where('is_delete', 'eq', '0')
			->where('mail_send_type', 'eq', '1')
			->where('mail_title|mail_content', 'like', '%' . $keyword . '%')
			->field('mail_title,mail_content,create_time,mail_id,is_delete')
			->select();
		$title = '公告';
		// 查询该id所对应的角色，组织，人员
		$oid = Db::table('cn_user_organization_relation')
			->alias('cuor')
			->join('cn_organization co', 'cuor.org_id = co.org_id')
			->where('cuor.user_id', $uid)
			->field('cuor.org_id')
			->find();
		$cid = Db::table('cn_user_character_relation')
			->alias('cucr')
			->join('cn_character cc', 'cucr.chara_id = cc.chara_id')
			->where('cucr.user_id', $uid)
			->field('cucr.chara_id')
			->find();

		// var_dump($num);die;
		// if ($oid['org_id']) {
		// 	$wsql = "SELECT
		//                       cm.mail_title,
		//                       cm.mail_id,
		//                       count( cm.mail_id ) AS count,
		//                       cm.mail_content,
		//                       cm.create_time,
		//                       cm.mail_id,
		//                       cms.STATUS
		//                   FROM
		//                       cn_mail AS cm
		//                       LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
		//                       LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//                   WHERE
		//                       cma.add_org_id = " . $oid['org_id'] . "

		//                       AND cm.is_delete = 0
		//                       AND cms.STATUS = 0
		//                       AND cm.mail_send_channel = 2
		//                       AND cms.user_id = " . $uid . "
		//                       and cms.status = 0
		//                   GROUP BY
		//                       cm.mail_id";
		// } else {
		$wsql = "SELECT
                        cm.mail_title,
                        cm.mail_id,
                        count( cm.mail_id ) AS count,
                        cm.mail_content,
                        cm.create_time,
                        cm.mail_id,
                        cms.STATUS
                    FROM
                        cn_mail AS cm
                        LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
                        LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                    WHERE
                        cms.user_id = " . $uid . "
                        AND cm.is_delete = 0
                        AND cms.STATUS = 0
                        AND cm.mail_send_channel = 2
                        and cms.status = 0
                    GROUP BY
                        cm.mail_id";
		// }
		$wres = Db::query($wsql);

		$num2 = (count($wres));

		// if (empty($num)) {
		// 	$num = 0;
		// 	if (empty($num2)) {
		// 		$num = 0;
		// 		$info = [
		// 			'list' => $list,
		// 			'res' => $res,
		// 			'num2' => $num2,
		// 			'keyword' => $keyword,
		// 			'title' => $title,
		// 		];
		// 	}
		// 	$info = [
		// 		'list' => $list,
		// 		'res' => $res,
		// 		'num2' => $num2['0']['count'],
		// 		'keyword' => $keyword,
		// 		'title' => $title,
		// 	];
		// } else {
		$info = [
			'list' => $list,
			'res' => $res,
			'num2' => $num2,
			'keyword' => $keyword,
			'title' => $title,
		];
		// }
		return $info;
	}

	public function getnMailinfo($id) {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '查看公告消息') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/getnMailinfo',
				'log_desc' => '查看公告消息',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();

		// 根据mail_id 查询  消息的标签  内容
		$mailinfo = Db::table('cn_mail_addressee')
			->alias('cma')
			->join('cn_mail cm', 'cm.mail_id = cma.mail_id')
			->where('cma.mail_id', $id)
			->where('cma.is_delete', 'eq', '0')
			->where('cm.is_delete', 'eq', '0')
			->field('cm.mail_content,cm.mail_title,mail_type,cma.create_user_id,cma.create_time,cma.mail_id')
			->find();
		//根据create_id查询发件人是谁
		$fjid = Db::table('cn_user')
			->alias('cu')
			->where('cu.user_id', $mailinfo['create_user_id'])
			->field('cu.user_name')
			->select();
		// 更改消息为已读
		$read = Db::table('cn_mail_status')
			->where('mail_id', $id)
			->where('user_id', $uid)
			->update(['status' => '1']);

		// 查询该id所对应的角色，组织，人员
		$oid = Db::table('cn_user_organization_relation')
			->alias('cuor')
			->join('cn_organization co', 'cuor.org_id = co.org_id')
			->where('cuor.user_id', $uid)
			->field('cuor.org_id')
			->find();
		$cid = Db::table('cn_user_character_relation')
			->alias('cucr')
			->join('cn_character cc', 'cucr.chara_id = cc.chara_id')
			->where('cucr.user_id', $uid)
			->field('cucr.chara_id')
			->find();

		// var_dump($num);die;
		// if ($oid['org_id']) {
		// 	$wsql = "SELECT
		//                       cm.mail_title,
		//                       cm.mail_id,
		//                       count( cm.mail_id ) AS count,
		//                       cm.mail_content,
		//                       cm.create_time,
		//                       cm.mail_id,
		//                       cms.STATUS
		//                   FROM
		//                       cn_mail AS cm
		//                       LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
		//                       LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//                   WHERE
		//                       cma.add_org_id = " . $oid['org_id'] . "

		//                       AND cm.is_delete = 0
		//                       AND cms.STATUS = 0
		//                       AND cm.mail_send_channel = 2
		//                       AND cms.user_id = " . $uid . "
		//                       and cms.status = 0
		//                   GROUP BY
		//                       cm.mail_id";
		// } else {
		$wsql = "SELECT
                        cm.mail_title,
                        cm.mail_id,
                        count( cm.mail_id ) AS count,
                        cm.mail_content,
                        cm.create_time,
                        cm.mail_id,
                        cms.STATUS
                    FROM
                        cn_mail AS cm
                        LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
                        LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                    WHERE
                        cms.user_id = " . $uid . "
                        AND cm.is_delete = 0
                        AND cms.STATUS = 0
                        AND cm.mail_send_channel = 2
                        and cms.status = 0
                    GROUP BY
                        cm.mail_id";
		// }
		$wres = Db::query($wsql);

		$num2 = (count($wres));
		// dump($num2);die;
		// if (empty($num)) {
		// 	$num = 0;
		// 	if (empty($num2)) {
		// 		$num2 = 0;
		// 		$info = [
		// 			'node' => $list,
		// 			'mailinfo' => $mailinfo,
		// 			'fjid' => $fjid,
		// 			'num2' => $num2,
		// 		];
		// 	}
		// 	$info = [
		// 		'node' => $list,
		// 		'mailinfo' => $mailinfo,
		// 		'fjid' => $fjid,
		// 		'num2' => $num2,
		// 	];
		// } else {
		$info = [
			'node' => $list,
			'mailinfo' => $mailinfo,
			'fjid' => $fjid,
			'num2' => $num2,
		];
		// }

		return $info;
	}

	// 已发消息
	public function getMailalready($id) {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];
		if ($prevdata !== '查看已发消息') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'Inbox/getMailalready',
				'log_desc' => '查看已发消息',
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$ires = db('_user_log')
				->insert($data);
		}
		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();

		// 根据消息id查询消息内容
		$mailinfo = Db::table('cn_mail_addressee')
			->alias('cma')
			->join('cn_mail cm', 'cm.mail_id = cma.mail_id')
			->where('cma.mail_id', $id)
			->where('cma.is_delete', 'eq', '0')
			->where('cm.is_delete', 'eq', '0')
			->field('cm.mail_content,cm.mail_title,mail_type,cma.create_time,cma.add_type,cma.add_org_id,cma.add_user_id,cma.add_chara_id')
			->find();
		// 收件人信息
		if ($mailinfo['add_type'] == 1) {
			$org_id = $mailinfo['add_org_id'];
			$sjid = Db::table('cn_organization')
				->alias('co')
				->where('co.org_id', $org_id)
				->field('co.org_name')
				->find();
			$sj = $sjid['org_name'];
		} elseif ($mailinfo['add_type'] == 2) {
			$chara_id = $mailinfo['add_chara_id'];
			$sjid = Db::table('cn_character')
				->alias('cc')
				->where('cc.chara_id', $chara_id)
				->field('cc.chara_name')
				->find();
			$sj = $sjid['chara_name'];
		} elseif ($mailinfo['add_type'] == 3) {
			$user_id = $mailinfo['add_user_id'];
			$sjid = Db::table('cn_user')
				->alias('cu')
				->where('cu.user_id', $user_id)
				->field('cu.user_name')
				->find();
			$sj = $sjid['user_name'];
		}

		// 查询该id所对应的角色，组织，人员
		$oid = Db::table('cn_user_organization_relation')
			->alias('cuor')
			->join('cn_organization co', 'cuor.org_id = co.org_id')
			->where('cuor.user_id', $uid)
			->field('cuor.org_id')
			->find();
		$cid = Db::table('cn_user_character_relation')
			->alias('cucr')
			->join('cn_character cc', 'cucr.chara_id = cc.chara_id')
			->where('cucr.user_id', $uid)
			->field('cucr.chara_id')
			->find();

		// var_dump($num);die;
		// if ($oid['org_id']) {
		// 	$wsql = "SELECT
		//                       cm.mail_title,
		//                       cm.mail_id,
		//                       count( cm.mail_id ) AS count,
		//                       cm.mail_content,
		//                       cm.create_time,
		//                       cm.mail_id,
		//                       cms.STATUS
		//                   FROM
		//                       cn_mail AS cm
		//                       LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
		//                       LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
		//                   WHERE
		//                       cma.add_org_id = " . $oid['org_id'] . "

		//                       AND cm.is_delete = 0
		//                       AND cms.STATUS = 0
		//                       AND cm.mail_send_channel = 2
		//                       AND cms.user_id = " . $uid . "
		//                       and cms.status = 0
		//                   GROUP BY
		//                       cm.mail_id";
		// } else {
		$wsql = "SELECT
                        cm.mail_title,
                        cm.mail_id,
                        count( cm.mail_id ) AS count,
                        cm.mail_content,
                        cm.create_time,
                        cm.mail_id,
                        cms.STATUS
                    FROM
                        cn_mail AS cm
                        LEFT JOIN cn_mail_addressee AS cma ON cma.mail_id = cm.mail_id
                        LEFT JOIN cn_mail_status AS cms ON cms.mail_id = cm.mail_id
                    WHERE
                        cms.user_id = " . $uid . "
                        AND cm.is_delete = 0
                        AND cm.mail_send_channel
                        AND cms.STATUS = 0= 2
                        and cms.status = 0
                    GROUP BY
                        cm.mail_id";
		// }
		$wres = Db::query($wsql);

		$num2 = (count($wres));
		if (empty($num)) {
			$num = 0;
			if (empty($num2)) {
				$num2 = 0;
				$info = [
					'node' => $list,
					'mailinfo' => $mailinfo,
					'sj' => $sj,
					'num2' => $num2,
				];
			}
			$info = [
				'node' => $list,
				'mailinfo' => $mailinfo,
				'sj' => $sj,
				'num2' => $num2['0'],
			];
		} else {
			$info = [
				'node' => $list,
				'mailinfo' => $mailinfo,
				'sj' => $sj,
				'num2' => $num2,
			];
		}

		return $info;
	}

}

?>